package com.zjl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zjl.common.BaseContext;
import com.zjl.common.R;
import com.zjl.entity.AddressBook;
import com.zjl.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址，注意需要先手动赋userid在保存
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        //传入的addressBook对象中，其UserID属性值为空，
        // 需要通过在同一线程的session得到其id，并为其赋上。否则，保存没有意义
        addressBook.setUserId(BaseContext.getCurrent());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址，注意，userId从线程中获得。
     * @param addressBook
     * @return
     */
    @PutMapping(value = "default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        //获得当前地址所属用户id
        Long userId = BaseContext.getCurrent();

        //修改该用户所有地址为非默认。
            //修改条件构造器
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
            //添加过滤条件
        updateWrapper.eq(AddressBook::getUserId,userId);
            //设置修改
        updateWrapper.set(AddressBook::getIsDefault,0);
            //执行修改
        addressBookService.update(updateWrapper);

        //设置userId
        addressBook.setUserId(userId);
        //设置默认地址标识
        addressBook.setIsDefault(1);

        //单独执行修改默认地址操作
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook==null){
            return R.error("id不存在");
        }
        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrent());
        wrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook==null)
            return R.error("该用户没有默认地址");

        return R.success(addressBook);
    }

    /**
     * 查询用户所有地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrent());
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null!=addressBook.getUserId(),AddressBook::getUserId,addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        return R.success(addressBookService.list(wrapper));
    }
}
